package com.campus.counseling.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.campus.counseling.entity.Student;
import com.campus.counseling.model.mapper.StudentMapper;
import com.campus.counseling.service.StudentService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student> implements StudentService {

    @Override
    public Student getByUserId(Long userId) {
        // 查询用户的学生信息，如果不存在则返回一个新的对象
        Student student = this.lambdaQuery()
            .eq(Student::getUserId, userId)
            .one();
            
        if (student == null) {
            // 如果不存在，返回一个新的Student对象，只设置userId
            student = new Student();
            student.setUserId(userId);
        }
        
        return student;
    }

    @Override
    @Transactional
    public void updateStudentInfo(Student student) {
        // 先查询是否存在
        Student existing = this.lambdaQuery()
            .eq(Student::getUserId, student.getUserId())
            .one();
            
        if (existing != null) {
            // 更新
            student.setId(existing.getId());
            this.updateById(student);
        } else {
            // 新增
            this.save(student);
        }
    }
} 